#include "test_check.h"
#include "ui_test_check.h"
#include "dialog_msbox.h"
test_check::test_check(test *da,QWidget *parent) :
    QDialog(parent),
    ui(new Ui::test_check)
{
    ui->setupUi(this);
    com_data=da;
    this->setWindowTitle("循环控制水温 温度均值<=0.1 适于试验样品");

    bush_time=new QTimer(this);
    connect(bush_time,&QTimer::timeout,this,[=]
    {
         ui->temp_air->setText(QString("%1 ℃").arg(com_data->temp_air,0,'f',3));
         ui->temp_water->setText(QString("%1 ℃").arg(com_data->temp_water,0,'f',3));
         ui->temp_waitong->setText(QString("%1 ℃").arg(com_data->temp_waitong,0,'f',3));
         ui->check_num->setText(QString("%1").arg(check_add));

         if(T_K.count()<600)
         {
             T_air.append(com_data->temp_air);
             T_water.append(com_data->temp_water);
             T_waitong.append(com_data->temp_waitong);
             T_K.append(T_K.count());
         }else
         {
             T_air.removeFirst();
             T_water.removeFirst();
             T_waitong.removeFirst();

             T_air.append(com_data->temp_air);
             T_water.append(com_data->temp_water);
             T_waitong.append(com_data->temp_waitong);
         }

         ui->plot->graph(0)->setData(T_K,T_air);
         ui->plot->graph(1)->setData(T_K,T_waitong);
         ui->plot->graph(2)->setData(T_K,T_water);

    });

    bush_time->start(1000);

    run_time=new QTimer(this);
    connect(run_time,&QTimer::timeout,this,&test_check::run_time_out);


    //曲线初始化
   ui->plot->addGraph();
   ui->plot->addGraph();
   ui->plot->addGraph();

   ui->plot->graph(0)->setPen(QPen(Qt::red));
   ui->plot->graph(1)->setPen(QPen(Qt::blue));
   ui->plot->graph(2)->setPen(QPen(Qt::black));

   ui->plot->setBackground(QBrush(QColor(255, 255, 255)));
   ui->plot->yAxis->setLabel("温度  /℃ ");
   ui->plot->xAxis->setLabel("Time  /s ");
   ui->plot->xAxis->ticker()->setTickCount(11);//设刻度数
   ui->plot->yAxis->ticker()->setTickCount(10);//设刻度数
   ui->plot->xAxis->setTickLengthIn(10);//设主刻度显示长度;
   ui->plot->yAxis->setTickLengthIn(10);//设主刻度显示长度;
   ui->plot->xAxis->setSubTickLength(6);//设分刻度显示长度;
   ui->plot->yAxis->setSubTickLength(6);//设分刻度显示长度;

   ui->plot->xAxis->setTickPen(QPen(QColor(Qt::black)));//设主刻度颜色
   ui->plot->yAxis->setTickPen(QPen(QColor(Qt::black)));  //设主刻度颜色
   ui->plot->xAxis->setSubTickPen(QPen(QColor(Qt::black)));//设分刻度颜色
   ui->plot->yAxis->setSubTickPen(QPen(QColor(Qt::black)));//设分刻度颜色
   ui->plot->xAxis->setBasePen(QPen(QColor(Qt::black)));//设x轴线颜色
   ui->plot->yAxis->setBasePen(QPen(QColor(Qt::black)));//设y轴线颜色

   ui->plot->xAxis->setTickLabelColor(Qt::black);//设主刻度标签颜色
   ui->plot->yAxis->setTickLabelColor(Qt::black);//设主刻度标签颜色

   ui->plot->xAxis->setLabelColor(Qt::black);//设X轴标签颜色
   ui->plot->yAxis->setLabelColor(Qt::red);//设y轴标签颜色

   QFont ft;
   ft.setPixelSize(20);
   ui->plot->xAxis->setLabelFont(ft);
   ui->plot->yAxis->setLabelFont(ft);
   QFont ft_tick;
   ft_tick.setPixelSize(15);
   ui->plot->xAxis->setTickLabelFont(ft_tick);
   ui->plot->yAxis->setTickLabelFont(ft_tick);

   ui->plot->xAxis->setRange(0,600);//设定X轴范围
   ui->plot->yAxis->setRange(20,30);//设定X轴范围


}

test_check::~test_check()
{
    delete ui;
}

void test_check::run_time_out()
{
    static int time_add=0;
    if(state==0)//不知道状态的情况下先放水40秒
    {
        QByteArray sm;
        sm.append(0x06);//开始排水
        com_data->buf.writedata.append(sm);
        state=1; ui->btn_start->setText("正在初始化");
    }else if(state==1)
    {
        if(time_add>45)
        {
            QByteArray sm;
            sm.append(0x0C);
            com_data->buf.writedata.append(sm);
            state=2;time_add=0;

        }else time_add++;

    }else if(state==2)//开始注水
    {
        QByteArray sm;
        sm.append(0x09);
        com_data->buf.writedata.append(sm);
        state=3;time_add=0;
        ui->textEdit->append(QString("%1 : 第%2次循环，开始注水")
             .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")).arg(check_add));

        ui->btn_start->setText("停止");

    }else if(state==3)
    {
         if(time_add>30)
         {
             state=4;  QByteArray sm;
             sm.append(0x0C);com_data->buf.writedata.append(sm);//停止注水
         }
         else  { time_add++; ui->progressBar->setValue(time_add);}
    }else if(state==4)
    {
        QByteArray sm;
        sm.append(0x06);//开始排水
        com_data->buf.writedata.append(sm);
        state=5;
        ui->textEdit->append(QString("%1 : 第%2次循环，开始排水")
             .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")).arg(check_add));

    }else if(state==5)
    {
        if(time_add>80)
        {
            state=2; check_add++; QByteArray sm;
            sm.append(0x0c);//停止排水
            sm.append(0x07);//停止排水
            com_data->buf.writedata.append(sm);//停止注水
        }
        else {time_add++; ui->progressBar->setValue(time_add);}

    }else if(state==6)//
    {
        time_add=0;state=7;
        ui->btn_start->setText("正在停止");
        QByteArray sm;
        sm.append(0x0c);//停止排水
        sm.append(0x07);//停止排水
        com_data->buf.writedata.append(sm);//停止注水

    }else if(state==7)
    {
        if(time_add>50)
        {
            run_time->stop();
            ui->btn_start->setText("开始");
            state=0;
        }else {time_add++; ui->btn_start->setText("正在停止");}
    }

}

void test_check::on_btn_start_clicked()
{
    if(!run_time->isActive())
    {
        run_time->start(1000);
        state=0;
    }else
    {
         state=6;
    }
}

void test_check::on_btn_stop_clicked()
{
   if(!run_time->isActive()) done(0);
   else Dialog_msbox::show(1,0,"退出","Exit",QString("请先停止控制，再退出"));

}
